﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Reflection;

namespace DevelopStudio.Basic
{
    [Serializable]
    [XmlRoot("Root")]
    public class RedmineInfo
    {
        public RedmineInfo()
        {
            this.Init();
        }

        private void Init()
        {
            BasicInfo = new BasicRedmineInfo();
            Members = new List<MemberInfo>();
            Projects = new List<ProjectInfo>();
        }

        [XmlElement("BasicInfo")]
        public BasicRedmineInfo BasicInfo { get; set; }

        [XmlArray("Members")]
        [XmlArrayItem("Member", typeof(MemberInfo))]
        public List<MemberInfo> Members { get; set; }

        [XmlArray("Projects")]
        [XmlArrayItem("Project", typeof(ProjectInfo))]
        public List<ProjectInfo> Projects { get; set; }

        public void AddMembers(Dictionary<string, string> members, Guid projectID)
        {
            foreach (KeyValuePair<string, string> var in members)
            {
                MemberInfo memberInfo = this.Members.Where(o => o.ID == var.Key).FirstOrDefault();

                if (memberInfo == null)
                {
                    memberInfo = new MemberInfo();
                    memberInfo.ID = var.Key;
                    memberInfo.Name = var.Value;
                    this.Members.Add(memberInfo);
                }

                if (memberInfo.OwnerProjects.Contains(projectID) == false)
                {
                    memberInfo.OwnerProjects.Add(projectID);
                }
            }
        }
    }

    [Serializable]
    [XmlRoot("Root")]
    public class BasicRedmineInfo : ICloneable
    {
        public BasicRedmineInfo()
        {
            Init();
        }

        private void Init()
        {
            BaseUrl = string.Empty;
            UserName = string.Empty;
            Password = string.Empty;
        }

        [XmlElement("BaseUrl")]
        public string BaseUrl { get; set; }

        [XmlElement("UserName")]
        public string UserName { get; set; }

        [XmlElement("Password")]
        public string EncryptPassword { get; set; }

        [XmlIgnore]
        public string Password
        {
            get
            {
                return SNSHelper.Common.SecurityUtility.DecryptDES(this.EncryptPassword, Utility.EncryptKeyString);
            }

            set
            {
                this.EncryptPassword = SNSHelper.Common.SecurityUtility.EncryptDES(value, Utility.EncryptKeyString);
            }
        }

        public object Clone()
        {
            MemoryStream stream = new MemoryStream();

            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(BasicRedmineInfo));
                serializer.Serialize(stream, this);
                stream.Flush();
                stream.Position = 0;
                BasicRedmineInfo newObject = serializer.Deserialize(stream) as BasicRedmineInfo;
                return newObject;
            }
            finally
            {
                stream.Dispose();
            }
        }
    }
}
